package com.magicborrow.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.utils.ImageLoadUtil;

import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
/**
 *   展示影片图片的act
 */
public class DetailPicActivity extends BaseActivity{
    private TextView textView1;
    private TextView textView2;
    private ViewPager vp_pic;
    private List<StuffBean.ImagesEntity> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_recycler_pic);
        if (getIntent().hasExtra("images")) {
            datas = (List<StuffBean.ImagesEntity>) getIntent().getSerializableExtra("images");
        }
        vp_pic = (ViewPager) findViewById(R.id.vp_pic);
        vp_pic.setOffscreenPageLimit(datas.size());
        vp_pic.setAdapter(new SamplePagerAdapter());
        vp_pic.setCurrentItem(0, false);

        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView1.setText("0");
        textView2.setText(datas.size() + "");
        if (datas.size()== 1) {
            textView1.setText("1");
            textView2.setText("1");
        }
    }

    class SamplePagerAdapter extends PagerAdapter {
        @Override
        public void startUpdate(ViewGroup container) {
            super.startUpdate(container);
            textView1.setText((vp_pic.getCurrentItem()+1)+"");
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final PhotoView photoView = new PhotoView(container.getContext());
            photoView.setMaxScale(10);
            String bigImageUrl = datas.get(position).getOrigin();
            photoView.setTag(bigImageUrl);
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View arg0, float arg1, float arg2) {
                    if (DetailPicActivity.this != null) {
                        finish();
                    }
                }
            });
            ImageLoadUtil.loadImage(photoView, Constants.ADDRESS+bigImageUrl,R.mipmap.empty_photo);
            container.addView(photoView,LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}
